package com.hxkcloud.patient.activitys;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.beans.PatientHistoryBean;
import com.hxkcloud.patient.beans.PatientHistoryDateBean;
import com.hxkcloud.patient.R;
import com.hxkcloud.patient.popup.CalendarPopWindow;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.ImageUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by william_sun on 2016-07-07.
 * Description:症状/诊疗，基本信息，既往史记录页
 */
public class SymptomRecordActivity extends BaseActivity implements View.OnClickListener {
    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private LinearLayout ll_date;
    private TextView tv_date;

    private LinearLayout ll_history;

    private CalendarPopWindow calendarView;

    private final int historytype_symptom = 0;
    private final int historytype_basicinfo = 1;
    private final int historytype_oldhistory = 2;

    private int historytype = historytype_symptom;
    private String patientId = "";

    private Calendar calendarCur;//当前日期

    private List<PatientHistoryDateBean> dates;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_SYMPTOMRECORDDATES_SUCCESS:
            case MessageCode.NETWORKCODE_BASICINFODATES_SUCCESS:
            case MessageCode.NETWORKCODE_OLDHISTORYDATES_SUCCESS:
                dates = (List<PatientHistoryDateBean>) msg.obj;
                if (dates != null && dates.size() > 0) {
                    showDates();
                } else {
                    toastMsg(R.string.nodata);
                }
                break;
            case MessageCode.NETWORKCODE_SYMPTOMRECORDDATES_FAILURE:
            case MessageCode.NETWORKCODE_BASICINFODATES_FAILURE:
            case MessageCode.NETWORKCODE_OLDHISTORYDATES_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
            case MessageCode.NETWORKCODE_SYMPTOMRECORDS_SUCCESS:
            case MessageCode.NETWORKCODE_BASICINFOS_SUCCESS:
            case MessageCode.NETWORKCODE_OLDHISTORYS_SUCCESS:
                ll_history.removeAllViews();
                List<PatientHistoryBean> historys = (List<PatientHistoryBean>) msg.obj;
                if (historys != null && historys.size() > 0) {
                    for (PatientHistoryBean history : historys) {
                        LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        TextView tv_name = new TextView(mContext);
                        tv_name.setLayoutParams(lp_tv);
                        tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                        tv_name.setTextSize(14);
                        tv_name.setText("【" + history.getName() + "】");
                        if (!TextUtils.isEmpty(history.getName())) {
                            ll_history.addView(tv_name);
                        }


                        String[] ss = history.getInfo().split("\n");
                        for (int i = 0; i < ss.length; i++) {
                            System.out.println(ss[i]);
                            LinearLayout.LayoutParams lp_tvinfo = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            TextView tv_info = new TextView(mContext);
                            lp_tvinfo.topMargin = ImageUtil.dip2px(4, mContext);
                            lp_tvinfo.leftMargin = ImageUtil.dip2px(5, mContext);
                            tv_info.setLayoutParams(lp_tvinfo);
                            tv_info.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                            tv_info.setTextSize(14);
                            tv_info.setText(ss[i]);
                            ll_history.addView(tv_info);
                        }


                        View view = new View(mContext);
                        LinearLayout.LayoutParams lp_view = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ImageUtil.dip2px(3, mContext));
                        lp_view.topMargin = ImageUtil.dip2px(8, mContext);
                        lp_view.bottomMargin = ImageUtil.dip2px(8, mContext);
                        view.setLayoutParams(lp_view);
                        view.setBackgroundResource(R.drawable.divider_dash);
                        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速


                        ll_history.addView(view);
                    }
                }
                break;
            case MessageCode.NETWORKCODE_SYMPTOMRECORDS_FAILURE:
            case MessageCode.NETWORKCODE_BASICINFOS_FAILURE:
            case MessageCode.NETWORKCODE_OLDHISTORYS_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_symptomrecord);

        historytype = this.getIntent().getIntExtra("type", historytype_symptom);
        patientId = this.getIntent().getStringExtra("patientId");

        initView();
        initListener();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        ll_date = (LinearLayout) findViewById(R.id.ll_symptomrecord_date);
        tv_date = (TextView) findViewById(R.id.tv_symptomrecord_date);
        ll_history = (LinearLayout) findViewById(R.id.ll_symptomrecord_history);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.historyrecord));

        calendarView = new CalendarPopWindow(this);
        calendarView.setCalendarListener(new CalendarPopWindow.CalendarListener() {
            @Override
            public void setCalendar(String calendar) {
                boolean exist = false;
                if (dates != null && dates.size() > 0) {
                    for (PatientHistoryDateBean d : dates) {
                        if (d.getDate().split(" ")[0].equals(calendar)) {
                            exist = true;
                        }
                    }
                }
                if (exist) {
                    tv_date.setText(calendar);
                    //get info
                    loading();
                    if (historytype == historytype_symptom) {
                        YiJiaHuNetWorkUtils.symptomRecords(patientId, calendar, handler);
                    } else if (historytype == historytype_basicinfo) {
                        YiJiaHuNetWorkUtils.basicInfos(patientId, calendar, handler);
                    } else {
                        YiJiaHuNetWorkUtils.oldHistorys(patientId, calendar, handler);
                    }
                } else {
                    toastMsg(R.string.nodata);
                }
            }
        });
    }


    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        ll_date.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        calendarCur = Calendar.getInstance();
        int curYear = calendarCur.get(Calendar.YEAR);
        int curMonth = calendarCur.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        int curDate = calendarCur.get(Calendar.DATE);

        tv_date.setText(curYear + "-" + curMonth + "-" + curDate);

        //获取时间列表
        loading();
        if (historytype == historytype_symptom) {
            YiJiaHuNetWorkUtils.symptomRecordDates(patientId, handler);
        } else if (historytype == historytype_basicinfo) {
            YiJiaHuNetWorkUtils.basicInfoDates(patientId, handler);
        } else {
            YiJiaHuNetWorkUtils.oldHistoryDates(patientId, handler);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.ll_symptomrecord_date:
                if (calendarView == null) {
                    calendarView = new CalendarPopWindow(this);
                }

                calendarView.showDropdown(ll_title);
                break;
        }
    }

    //日历上标注点
    private void showDates() {
        List<Calendar> calendars = new ArrayList<Calendar>();
        for (PatientHistoryDateBean d : dates) {
            Calendar c = Calendar.getInstance();
            int year = Integer.valueOf(d.getDate().split(" ")[0].split("-")[0]);
            int month = Integer.valueOf(d.getDate().split(" ")[0].split("-")[1]) - 1;
            int day = Integer.valueOf(d.getDate().split(" ")[0].split("-")[2]);
            c.set(year, month, day);
            calendars.add(c);
        }
        calendarView.setPoints(calendars, 0);

        tv_date.setText(dates.get(dates.size() - 1).getDate().split(" ")[0]);
        //get info
        loading();
        if (historytype == historytype_symptom) {
            YiJiaHuNetWorkUtils.symptomRecords(patientId, dates.get(dates.size() - 1).getDate(), handler);
        } else if (historytype == historytype_basicinfo) {
            YiJiaHuNetWorkUtils.basicInfos(patientId, dates.get(dates.size() - 1).getDate(), handler);
        } else {
            YiJiaHuNetWorkUtils.oldHistorys(patientId, dates.get(dates.size() - 1).getDate(), handler);
        }
    }

}
